package com.eric.husky.service;

import com.eric.husky.common.utils.UpdateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.eric.husky.dao.EaccountDao;
import com.eric.husky.bean.Eaccount;
import java.util.List;

@Service
public class EaccountService {
    @Autowired
	private EaccountDao eaccountDao;

	public Page<Eaccount> list(Eaccount eaccount, Pageable page) {
		Example<Eaccount> example = Example.of(eaccount);
		return eaccountDao.findAll(example, page);
	}

	public Eaccount get(Integer eaccountid) {
		return eaccountDao.findById(eaccountid).get();
	}

    public List<Eaccount> findList() {
		return eaccountDao.findAll();
	}

	@Transactional
	public Eaccount save(Eaccount eaccount) {
	    if(eaccount.getEaccountid()!=null){
	        Eaccount eaccountOld = eaccountDao.findById(eaccount.getEaccountid()).orElse(null);
	        if(eaccountOld!=null){
	            UpdateUtil.copyNullProperties(eaccount,eaccountOld);
                return eaccountDao.save(eaccountOld);
	        }
	    }
	    return eaccountDao.save(eaccount);
	}

	@Transactional
	public void delete(Integer eaccountid) {
		eaccountDao.deleteById(eaccountid);
	}

}